Public Class EditArtist

    Private _newArtists As New System.Collections.ObjectModel.Collection(Of String)
    Private _deletedArtists As New System.Collections.ObjectModel.Collection(Of Integer)

    Private _fileId As Integer

    Private Const ArtistNameRequiredMessage As String = "An Artist name must be provided."

    Public Event ArtistsChanged()

    Private Sub EditArtist_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.VendorTableAdapter.Fill(Me.VendorData.Vendor)

        FileArtistTableAdapter.Fill(FileArtistData.FileArtist, _
                                    _fileId)

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click

        FileArtistDS.CancelEdit()
        Me.Close()

    End Sub

    Private Sub UpdateArtists_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateArtists.Click

        Const ExpectedMessage As String = "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."

        Dim updated As Integer

        For Each deletedArtist As Integer In _deletedArtists
            Me.FileArtistTableAdapter.Delete(deletedArtist)
        Next

        FileArtistDS.EndEdit()
        Try

            updated = Me.FileArtistTableAdapter.Update(Me.FileArtistData)


        Catch ex As OleDb.OleDbException
            If ex.Message <> ExpectedMessage Then
                MsgBox(ex.Message)
            End If
        End Try

        RaiseEvent ArtistsChanged()

        Me.Close()

    End Sub

    Private Sub AddArtist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddArtist.Click

        'If ArtistName.SelectedValue > 0 Then
        Dim newFileArtist As FileArtistData.FileArtistRow = FileArtistData.FileArtist.NewFileArtistRow
        newFileArtist.artist = ArtistName.SelectedValue.ToString
        newFileArtist.file_id = _fileId
        Try
            FileArtistData.FileArtist.AddFileArtistRow(newFileArtist)
            _newArtists.Add(ArtistName.Text)
        Catch ex As Exception

        End Try
        ArtistList.Refresh()
        'Else
        'MsgBox(ArtistNameRequiredMessage)
        'End If

    End Sub

    Public Sub New(ByVal fileId As Integer)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _fileId = fileId

    End Sub

    Private Sub RemoveArtist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveArtist.Click

        Dim entryRow As FileArtistData.FileArtistRow
        Dim rowsToDelete As New System.Collections.ObjectModel.Collection(Of FileArtistData.FileArtistRow)

        For Each entry As System.Data.DataRowView In ArtistList.SelectedItems
            entryRow = CType(entry.Row, FileArtistData.FileArtistRow)
            If entryRow.RowState = DataRowState.Added Then
                _newArtists.Remove(entryRow.artist)
            Else
                _deletedArtists.Add(entryRow.fa_id)
            End If
            rowsToDelete.Add(entryRow)
        Next

        For Each entryRow In rowsToDelete
            FileArtistData.FileArtist.RemoveFileArtistRow(entryRow)
        Next

        ArtistList.Refresh()

    End Sub

End Class